/**
 *
 */

var uploadify_onSelectError = function(file, errorCode, errorMsg) {
    var msgText = "上传失败\n";
    switch (errorCode) {
        case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
            //this.queueData.errorMsg = "每次最多上传 " + this.settings.queueSizeLimit + "个文件";
            msgText += "每次最多上传 " + this.settings.queueSizeLimit + "个文件";
            break;
        case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
            msgText += "文件大小超过限制( " + this.settings.fileSizeLimit + " )";
            break;
        case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
            msgText += "文件大小为0";
            break;
        case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
            msgText += "文件格式不正确，仅限 " + this.settings.fileTypeExts;
            break;
        default:
            msgText += "错误代码：" + errorCode + "\n" + errorMsg;
    }
    alert(msgText);
};

var uploadify_onUploadError = function(file, errorCode, errorMsg, errorString) {  
    // 手工取消不弹出提示  
    if (errorCode == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED  
            || errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED) {  
        return;  
    }  
    var msgText = "上传失败\n";  
    switch (errorCode) {  
        case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:  
            msgText += "HTTP 错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:  
            msgText += "上传文件丢失，请重新上传";  
            break;  
        case SWFUpload.UPLOAD_ERROR.IO_ERROR:  
            msgText += "IO错误";  
            break;  
        case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:  
            msgText += "安全性错误\n" + errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:  
            msgText += "每次最多上传 " + this.settings.uploadLimit + "个";  
            break;  
        case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:  
            msgText += errorMsg;  
            break;  
        case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:  
            msgText += "找不到指定文件，请重新操作";  
            break;  
        case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:  
            msgText += "参数错误";  
            break;  
        default:  
            msgText += "文件:" + file.name + "\n错误码:" + errorCode + "\n"  
                    + errorMsg + "\n" + errorString;  
    }  
    alert(msgText);  
    //return parameters;  
};

function UploadFile(option){
	if(option.width == undefined || option.width == null || option.width == ""){
		option.width = '70' ;
	}
	if(option.height == undefined || option.height == null || option.height == ""){
		option.height = '23' ;
	}
	if(option.defaultImg == undefined || option.defaultImg == null || option.defaultImg == ""){
		option.defaultImg = '/assets/js/huploadify/images/choose-file.png' ;
	}
	if(option.fileTypeExts == undefined || option.fileTypeExts == null || option.fileTypeExts == ""){
		option.fileTypeExts = '*.*' ;
	}
	if(option.fileTypeDesc == undefined || option.fileTypeDesc == null || option.fileTypeDesc == ""){
		option.fileTypeDesc = '所有文件' ;
	}
	if(option.queueSizeLimit == undefined || option.queueSizeLimit == null || option.queueSizeLimit == ""){
		option.queueSizeLimit = 1 ;
	}
	if(option.multi == undefined || option.multi == null || option.multi == ""){
		option.multi = false ;
	}
	option.width,
	$("#"+option.fileinputID).Huploadify({
        'swf' 		: option.contextPath+'/assets/js/huploadify/uploadify.swf',
        'cancelImg' : option.contextPath+'/assets/js/huploadify/images/uploadify-cancel.png',
        'uploader' : option.contextPath+'/upload',
        'queueID' : 'fileQueue',//文件在页面的显示队列的id
        'queueSizeLimit' : option.queueSizeLimit,//可上传文件的个数
        'auto' : false,
        'method':'post',
        'width'  : option.width,
        'height'  : option.height,
        'progressData':'speed',//显示进度条
        'removeTimeout':0.5,
        'multi' : option.multi,
        'fileSizeLimit': '1024MB',//限制上传文件大小
        'buttonText' : '请选择文件',
        'fileTypeExts' : option.fileTypeExts,
        'fileTypeDesc' : option.fileTypeDesc,
        'buttonImage' : option.contextPath+option.defaultImg,
        'buttonClass' : '',
        'overrideEvents' : [ 'onInit','onDialogClose','onUploadSuccess', 'onUploadError', 'onSelectError' ],  
	    'onSelect' : function(){
			   $("#"+option.fileinputID).uploadify('upload','*');
		},
	    'onCancel':function(file){
	    	alert('文件：'+file.name+'取消！')
	    },
	    'onSelectError' : uploadify_onSelectError,  
	    'onUploadError' : uploadify_onUploadError,  
	    'onUploadSuccess' : function(file, data, response) { //成功提醒
        	if(data){
        		console.log("data:"+data);
        		var jsonResult = eval('('+data+')');
        		option.success(jsonResult);
        	}
        },
        'onInit' : function(){
        	$('#'+option.fileinputID+'-button').css("text-indent","0px");
        	$('#'+option.fileinputID+'-button').css("background-image","");
        	$('#'+option.fileinputID+'-button').css("text-align","left");
        	$('#'+option.fileinputID+'-button').html("<img src='"+option.contextPath+option.defaultImg+
        			"' border='0' style='width:"+option.width+"px;height:"+option.height+"px'/>");
        }
	});
}
	
/**
 * 计算并返回图片缩放后的参数
 * @param maxWidth 最大宽度
 * @param maxHeight 最大高度
 * @param width 照片实际宽度
 * @param height 照片实际高度
 */
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
	var param = {top:0, left:0, width:width, height:height};
	if(width>maxWidth || height>maxHeight ) {
		rateWidth = width / maxWidth;
		rateHeight = height / maxHeight;
		if( rateWidth > rateHeight ) {
			param.width = maxWidth;
			param.height = Math.round(height / rateWidth);
		} else {
			param.width = Math.round(width / rateHeight);
			param.height = maxHeight;
		}
	}
	param.left = Math.round((maxWidth - param.width) / 2);
	param.top = Math.round((maxHeight - param.height) / 2);
	return param;
}

/**
 * 等比例压缩图片
 * @param maxWidth 最大宽度
 * @param maxHeight 最大高度
 * @param imgObj 需要缩放的img对象
 */
function resizePreview(maxWidth, maxHeight, imgObj) {
	var newImg = new Image();
	newImg.src = imgObj.src;
	var para = clacImgZoomParam(maxWidth, maxHeight, newImg.width, newImg.height);
	imgObj.style.width = para.width+'px';
	imgObj.style.height = para.height+'px';
	imgObj.style.marginLeft = para.left+'px';
	imgObj.style.marginTop = para.top+'px';
}

function Pause(obj,iMinSecond){
	if (window.eventList==null) window.eventList=new Array();
	var ind=-1;
 	for (var i=0;i<window.eventList.length;i++){
 		if (window.eventList[i]==null) {
  			window.eventList[i]=obj;
  			ind=i;
  			break;
 		}
 	}
  
 	if (ind==-1){
 		ind=window.eventList.length;
 		window.eventList[ind]=obj;
 	}
 	setTimeout("GoOn(" + ind + ")",1000);
}

function GoOn(ind){
	var obj=window.eventList[ind];
	window.eventList[ind]=null;
	if (obj.NextStep) obj.NextStep();
	else obj();
}